package reggie.ruiji.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reggie.ruiji.common.R;
import reggie.ruiji.pojo.Category;
import reggie.ruiji.pojo.PageBean;
import reggie.ruiji.service.CategoryService;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    @PostMapping
    public R<String> save(@RequestBody  Category category, HttpServletRequest request)//新增分类
    {

       categoryService.save(category,request);
       log.info("category:{}",category);
      return R.success(":新增分类成功");
    }

    @GetMapping("/page")
    public R<PageBean> getEmps(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize)
    {
        PageBean S=categoryService.pagee(page,pageSize);
        return R.success(S);
    }

    @DeleteMapping
    public R<String> delete(Long ids)//根据id删除分类
    {
        log.info(ids.toString());
        categoryService.delete(ids);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category, HttpServletRequest request)//修改分类
    {
        log.info("修改分类信息：{}",category);
        categoryService.update(category,request);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Category category)//根据条件查询
    {
        List<Category> S=categoryService.list(category.getType());
        return R.success(S);
    }

}